package com.szlanyou.thirdsdk.retrofit.callback;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈章
 * create at 2021/7/12 9:31
 * desc:
 * 用于缓存{@link IFileUploadProgressHttpResultHandler},
 * 然后统一在拦截器{@link com.szlanyou.thirdsdk.retrofit.interceptor.RequestProgressInterceptor}里回调{@link IFileUploadProgressHttpResultHandler#onRequestProgress(String, long, long)}
 */
public class FileUploadProgressHttpResultHandlerHolder {
    private static Map<String,IFileUploadProgressHttpResultHandler> fileUploadProgressHttpResultHandlerList = new HashMap<>();

    public static void add(@NonNull  String uniqueUploadFileRequestId, @NonNull IFileUploadProgressHttpResultHandler fileUploadProgressHttpResultHandler) {
        fileUploadProgressHttpResultHandlerList.put(uniqueUploadFileRequestId,fileUploadProgressHttpResultHandler);
    }

    public static void remove(@NonNull  String uniqueUploadFileRequestId) {
        fileUploadProgressHttpResultHandlerList.remove(uniqueUploadFileRequestId);
    }

    public static boolean isEmpty(){
        return  fileUploadProgressHttpResultHandlerList.isEmpty();
    }

    public static void onRequestProgress(String uniqueUploadFileRequestId,String url, long bytesWritten, long contentLength){
        IFileUploadProgressHttpResultHandler iFileUploadProgressHttpResultHandler = fileUploadProgressHttpResultHandlerList.get(uniqueUploadFileRequestId);
        if(iFileUploadProgressHttpResultHandler != null){
            iFileUploadProgressHttpResultHandler.onRequestProgress(url,bytesWritten,contentLength);
        }
    }

}
